package com.shqcjd.generalplatformprojectclient.ui.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shqcjd.generalplatformprojectclient.R;
import com.shqcjd.generalplatformprojectclient.api.ApiService;
import com.shqcjd.generalplatformprojectclient.helper.rxjavahelper.RxObserver;
import com.shqcjd.generalplatformprojectclient.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.generalplatformprojectclient.model.ResponseData;
import com.shqcjd.generalplatformprojectclient.model.pojo.PasswordBean;
import com.shqcjd.generalplatformprojectclient.ui.base.BaseActivity;
import com.shqcjd.generalplatformprojectclient.ui.base.BasePresenter;
import com.shqcjd.generalplatformprojectclient.util.PrefUtils;
import com.shqcjd.generalplatformprojectclient.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 更改密码
 */
public class ChangePasswordActivity extends BaseActivity {

    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.et_original_pwd)
    EditText etOriginalPwd;
    @BindView(R.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_change_password;
    }

    @Override
    public void initView() {
        tvToolbarCenter.setText("修改登录密码");
    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                String password = etOriginalPwd.getText().toString().trim();
                String newPassword = etNewPwd.getText().toString().trim();
                String newPasswordRepeat = etConfirmPwd.getText().toString().trim();
                if(newPasswordRepeat.equals(newPassword)){
                    if(newPasswordRepeat.length()>=6){
                        PasswordBean passwordBean = new PasswordBean();
                        passwordBean.id = PrefUtils.getInt(ChangePasswordActivity.this, PrefUtils.USER_USER_ID, 0);
                        passwordBean.newPassword = newPassword;
                        passwordBean.repeat = newPasswordRepeat;
                        passwordBean.oldPassword = password;
                        Gson g = new Gson();
                        String json = g.toJson(passwordBean);
                        updatePassword(json);
                    }else{
                        ToastUtils.showShort(this, "密码必须大于等于6位");
                    }
                }else{
                    ToastUtils.showShort(this, "请确认两次密码输入一致");
                }
                break;
        }
    }


    private void updatePassword(String json){
        ApiService.updatePassword(json)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        showWaitingDialog("修改中...");
                    }

                    @Override
                    public void _onNext(ResponseData listResponseData) {
                        if(listResponseData.code==0){
                            ToastUtils.showShort(ChangePasswordActivity.this, "修改成功");
                            finish();
                        }else{
                            ToastUtils.showShort(ChangePasswordActivity.this, listResponseData.message);
                        }
                        hideWaitingDialog();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }
}
